package com.itheima.goods.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.goods.dao.CategoryMapper;
import com.itheima.goods.pojo.Category;
import com.itheima.goods.service.CategoryService;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @package com.itheima.goods.controller
 * @description:
 * @zuozhe: 刘文博
 * @date: 2021/5/820:41
 * @vesion: v 1.0
 */
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private CategoryMapper categoryMapper;


    @GetMapping()
   public Result findAll(){
        List<Category> list = categoryService.list();
        return new Result(true, StatusCode.OK,"查询成功",list);
    }
    @PostMapping()
    public Result add(@RequestBody Category category){

      categoryService.save(category);
      return new Result(true,StatusCode.OK,"增加成功");
    }
    @GetMapping("/search/{page}/{size}")
    public Result findPage(@PathVariable("page")Integer page,@PathVariable("size")Integer size){
        Page<Category> categoryPage = new Page<>(page, size);
        QueryWrapper<Category> categoryQueryWrapper = new QueryWrapper<>();
        Page<Category> categoryPage1 = categoryMapper.selectPage(categoryPage, categoryQueryWrapper);
        List<Category> list = categoryPage1.getRecords();
        return new Result(true,StatusCode.OK,"查询成功",list);
    }
    @PostMapping("/search/{page}/{size}")
    public Result findPage(@PathVariable("page")Integer page,@PathVariable("size")Integer size,@RequestBody Object object){
        Page<Category> categoryPage = new Page<>(page, size);
        QueryWrapper<Category> categoryQueryWrapper = new QueryWrapper<>();
        Page<Category> categoryPage1 = categoryMapper.selectPage(categoryPage, categoryQueryWrapper);
        List<Category> list = categoryPage1.getRecords();
        return new Result(true,StatusCode.OK,"查询成功",list);
    }
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id")Integer id){
        Category category = categoryService.getById(id);
        return new Result(true,StatusCode.OK,"查询成功",category);
    }
    @PutMapping("/{id}")
    public Result update(@PathVariable("id")Integer id,@RequestBody Category category){
        LambdaQueryWrapper<Category> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Category::getId,id);
        boolean update = categoryService.update(category, wrapper);
        if (update){
            return new Result(true,StatusCode.OK,"更新成功");
        }else {
            return new Result(false,StatusCode.ERROR,"更新失败");
        }

    }
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id")Integer id){
        categoryService.removeById(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    }



